import { fetchApi } from './client.ts';
import type { AdminUsersData, UserInfo } from '../../../server/shared/types.ts';

interface CreateUserData {
  username: string;
  password: string;
}

interface UpdateUserData {
  username?: string;
  password?: string;
}

export const usersApi = {
  getUsers: () => 
    fetchApi<AdminUsersData>('/admin/users', {
      method: 'GET',
      headers: {
        Authorization: `Bearer ${localStorage.getItem('token')}`
      }
    }),
    
  createUser: (data: CreateUserData) =>
    fetchApi<{ user: UserInfo }>('/admin/users', {
      method: 'POST',
      headers: {
        Authorization: `Bearer ${localStorage.getItem('token')}`
      },
      body: JSON.stringify(data)
    }),
    
  updateUser: (id: number, data: UpdateUserData) =>
    fetchApi<{ user: UserInfo }>(`/admin/users/${id}`, {
      method: 'PUT',
      headers: {
        Authorization: `Bearer ${localStorage.getItem('token')}`
      },
      body: JSON.stringify(data)
    }),
    
  deleteUser: (id: number) =>
    fetchApi(`/admin/users/${id}`, {
      method: 'DELETE',
      headers: {
        Authorization: `Bearer ${localStorage.getItem('token')}`
      }
    })
}; 